<template>
  <div class="header-box">
    <el-menu default-active="1" mode="horizontal" active-text-color="#59a869" menu-trigger="click">
      <el-row>
        <el-col :span="17" class="parent">
          <div class="header-left">
            <div class="logo" @click="goto('/','Index')">
              <span class="logo-s">S</span>
              <span class="logo-j">J</span>
            </div>
            <div class="search">
              <el-input v-model="keyword" prefix-icon="el-icon-search" size="mini" placeholder="请输入搜索内容...">
                <template slot="suffix">
                  <i class="el-icon-check search-icon"></i>
                </template>
              </el-input>
            </div>
          </div>
        </el-col>
        <el-col :span="7">
          <el-row>
            <el-col :span="rightSpan">
              <el-menu-item index="1" @click="goto('/','Index')">
                <i class="iconfont icon-home-house"></i>&nbsp;主页
              </el-menu-item>
            </el-col>
            <el-col :span="rightSpan">
              <el-menu-item index="2" @click="goto('/tag/blogList' , 'TagBlogList')"><i class="iconfont icon-tag-black"></i>&nbsp;标签</el-menu-item>
            </el-col>
            <el-col :span="rightSpan">
              <el-menu-item index="4"><i class="iconfont icon-about-i"></i>&nbsp;关于</el-menu-item>
            </el-col>
            <el-col :span="4" v-show="!isLogin">
              <el-menu-item index="5" @click="goto('/login','Login')">
                <i class="iconfont icon-login"></i>&nbsp;登录
              </el-menu-item>
            </el-col>
            <el-col :span="4" v-show="isLogin">
              <el-submenu index="6" popper-class="user-panel">
                <template slot="title">
                  <el-avatar size="small" :src="avatarUrl"></el-avatar>
                </template>
                <el-menu-item index="6-1"><i class="iconfont icon-username user-icon"></i>&nbsp;{{ showName }}
                </el-menu-item>
                <el-menu-item index="6-2" @click="goto('/admin/myself','MySelf')">
                  <i class="iconfont icon-userInfo-db user-icon"></i>&nbsp;资料
                </el-menu-item>
                <el-menu-item index="6-3" @click="logout()"><i class="iconfont icon-exit user-icon"></i>&nbsp;退出
                </el-menu-item>
              </el-submenu>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-menu>
  </div>
</template>

<script>
export default {
  name: "Header",
  data() {
    return {
      keyword: "",
      rightSpan: 5,
      circleUrl: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
    }
  },
  computed: {
    showName() {
      let user = this.$store.getters.getUserInfo;
      if (user) {
        return user.nickname ? user.nickname : user.username;
      }
      return "您还未登录";
    },
    isLogin() {
      return this.$store.getters.getUserInfo;
    },
    avatarUrl() {
      if (this.isLogin) {
        return this.$store.getters.getUserInfo.avatar;
      }
    }
  },
  methods: {
    goto(path, name) {
      this.$router.push({
        path, name
      })
    },
    logout() {
      this.$swal({
        title: "你确定要退出吗？",
        icon: "warning",
        buttons: true,
        dangerMode: true,
      }).then((value) => {
        if (value) {
          this.$get(this.$api.LOGOUT_URL, {}).then(res => {
            if (res.data.code == 200) {
              this.$notify({
                title: "退出成功",
                type: "success",
                position: "top-right",
                customClass: "notify-position"
              })
              this.$store.commit("setToken", null);
              this.$store.commit("setUserInfo", null);
              if (this.$route.path.indexOf('admin') > -1) {
                this.$router.push({
                  path: '',
                  name: 'Index'
                });
              }
            }
          })
        } else {
          swal("这是一个明智的选择！", "", 'success');
        }
      });
    }
  }
}
</script>

<style scoped>

.el-menu {
  box-shadow: 0 0 5px 1px #d7d9d3;
  margin-bottom: 2vh;
}

.el-menu-item {
  text-align: center;
  font-weight: bolder;
  font-size: 0.9em;
}

.el-menu-item:hover {
  background: none;
}

.el-menu-item:focus {
  background: none;
  color: #59a869;
}

.header-left {
  height: 6vh;
  margin-left: 5vw;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  box-sizing: border-box;
  cursor: pointer;
}

.logo-s {
  font-size: 2em;
  font-weight: bolder;
  color: #59a869;
  font-family: 'Georgia';
  font-style: oblique;
}

.logo-j {
  display: block;
  font-size: 1.8em;
  font-weight: bolder;
  color: #0fc6bf;
  font-family: 'Lucida Handwriting';
  font-style: oblique;
  padding-top: 5px;
}

.search-icon {
  margin-top: 55%;
  cursor: pointer;
  transition: color .5s;
}

.search-icon:hover, .search-icon:focus {
  color: #59a869;
}

.user-icon {
  font-size: 16px !important;
}

.header-box {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99;
  box-sizing: border-box;
  width: 100vw;
  height: 6vh;
}

</style>
